Skip to content

feat(applocales): add get_locale_string_from_xaml helper#3371

Open
Wurschdhaud wants to merge 6 commits into
pyrevitlabs:developfrom
Wurschdhaud:feat/xaml-locale-helper
Open

feat(applocales): add get_locale_string_from_xaml helper#3371
Wurschdhaud wants to merge 6 commits into
pyrevitlabs:developfrom
Wurschdhaud:feat/xaml-locale-helper

Conversation

@Wurschdhaud
Copy link
Copy Markdown
Contributor

@Wurschdhaud Wurschdhaud commented May 9, 2026

Description

  • Adds get_locale_string_from_xaml(xaml_base_path, key) to applocales.py
    resolves a string from a *.ResourceDictionary.{locale}.xaml file with the
    same locale-then-English-then-key fallback as get_locale_string
  • Removes the manual load_resource_string / get_translated_ui_string
    functions from Preflight Checks and replaces them with the new helper
  • Replaces three WPFWindow temp-form constructions in Section Box Navigator
    __main__ (used only to call get_locale_string before the real form opens)
    with a lightweight _t() closure; tightens the surrounding error handling

EDIT:

refactor(preflight-checks): replace check_translations with applocales helper

  • Removes check_translations.py — the module that used a hardcoded cross-bundle path to read locale strings from the pushbutton folder
  • All check-specific strings now live in checks/locale/Checks.ResourceDictionary.{locale}.xaml, read via the existing applocales.get_locale_string_from_xaml helper (DRY — same API already used everywhere else in this branch)
  • Pushbutton ResourceDictionary files are trimmed to the four strings they actually own (the selection dialog UI); check output strings no longer live there
  • DocstringMeta metaclass replaced by plain name = _t(...) class attributes and ModelChecker.doc = _t(...) — same evaluation timing, zero magic
  • Fixes extensions.components.get_checks() to filter .py files only, preventing IronPython from trying to compile XAML assets found in checks/

Checklist

Before submitting your pull request, ensure the following requirements are met:

  • Code follows the PEP 8 style guide.
  • Code has been formatted with Black using the command:
    pipenv run black {source_file_or_directory}
  • Changes are tested and verified to work as expected.

Wurschdhaud and others added 3 commits May 9, 2026 11:25
Resolves locale strings from XAML ResourceDictionary files without
requiring a WPF form instance. Tries all locale codes of the current
locale, falls back to English, then returns the key as a final fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…es helper

Removes load_resource_string and get_translated_ui_string functions.
ask_for_preflight_checks now calls applocales.get_locale_string_from_xaml
directly. PreflightSelectFromList._setup (merge_resource_dict for WPF
template binding) is unchanged.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ain__

Replaces three WPFWindow temp_form constructions (used only to call
get_locale_string before the real form opened) with a _t() closure
backed by applocales.get_locale_string_from_xaml. Removes the
raise-Exception control-flow trick; inner try-except eliminated.
Outer except now logs with logger.exception then alerts directly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
devloai[bot]

This comment was marked as outdated.

@Wurschdhaud Wurschdhaud marked this pull request as draft May 9, 2026 11:10
Wurschdhaud and others added 2 commits May 10, 2026 10:50
The key is interpolated directly into an XPath attribute predicate surrounded by single quotes. A key containing ' raises xml.etree.ElementTree.ParseError, which is silently swallowed by the blanket except Exception: pass, causing the lookup to silently return the raw key. The fix is to use element iteration with Clark-notation attribute comparison instead of XPath string predicates
…s helper

- Delete check_translations.py and its sys.path manipulation, DocstringMeta
  metaclass, and hardcoded cross-bundle path to the pushbutton folder
- Add checks/locale/Checks.ResourceDictionary.{en_us,de_de,fr_fr,es_es,
  pt_br,ru,chinese_s}.xaml holding all check-specific strings; pushbutton
  ResourceDictionary files now carry only the four UI strings they own
  (AuthorLabel, ExtensionLabel, SelectPreflightCheckTitle, RunCheckButton)
- Each check module defines a module-level _t() helper that calls
  applocales.get_locale_string_from_xaml with a locale/ relative path,
  replacing all get_check_translation() call sites
- Replace DocstringMeta pattern with plain class attributes:
  name = _t(...) and ModelChecker.__doc__ = _t(...) — same evaluation
  timing, no metaclass magic
- Fix extensions.components.get_checks() to filter .py files only,
  preventing imp.load_source from being called on XAML assets in checks/

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Wurschdhaud Wurschdhaud marked this pull request as ready for review May 10, 2026 17:00
@jmcouffin jmcouffin requested a review from Copilot May 11, 2026 21:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends pyRevit’s localization utilities by adding a reusable XAML ResourceDictionary string lookup helper, then refactors existing tools (Preflight Checks + several check scripts + Section Box Navigator launch-time alerts) to use the shared helper instead of bespoke XML parsing / temporary WPF window instantiation. It also hardens extension check discovery to avoid treating non-Python assets as check scripts.

Changes:

  • Add applocales.get_locale_string_from_xaml(xaml_base_path, key) to resolve localized strings from *.ResourceDictionary.{locale}.xaml with fallback behavior.
  • Refactor Preflight Checks and multiple extensions/pyRevitTools.extension/checks/*.py modules to read translations from dedicated checks/locale/Checks.ResourceDictionary.*.xaml files via the new helper; remove check_translations.py.
  • Update extension check discovery to only return .py files, avoiding XAML asset compilation attempts.

Reviewed changes

Copilot reviewed 30 out of 30 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pyrevitlib/pyrevit/extensions/components.py Filter get_checks() results to .py files only to avoid treating XAML/assets as check scripts.
pyrevitlib/pyrevit/coreutils/applocales.py Add get_locale_string_from_xaml helper for localized XAML ResourceDictionary string lookup.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/script.py Replace ad-hoc XML parsing translation helpers with applocales.get_locale_string_from_xaml.
extensions/pyRevitTools.extension/pyRevit.tab/Modify.panel/3D.pulldown/Section Box Navigator.pushbutton/script.py Replace temp-form construction used only for translation lookup with a lightweight _t() closure.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.en_us.xaml Trim pushbutton-owned ResourceDictionary to only selection dialog strings.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.de_de.xaml Same trimming for German resources.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.es_es.xaml Same trimming for Spanish resources.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.fr_fr.xaml Same trimming for French resources.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.pt_br.xaml Same trimming for Brazilian Portuguese resources.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.ru.xaml Same trimming for Russian resources.
extensions/pyRevitTools.extension/pyRevit.tab/Project.panel/Preflight Checks.pushbutton/PreflightCheckTemplate.ResourceDictionary.chinese_s.xaml Same trimming for Simplified Chinese resources.
extensions/pyRevitTools.extension/checks/check_translations.py Remove legacy translation module that used cross-bundle path assumptions and a metaclass for docstrings/names.
extensions/pyRevitTools.extension/checks/grids_check.py Switch UI strings to _t() backed by new Checks locale dictionaries; simplify class name/__doc__ assignment.
extensions/pyRevitTools.extension/checks/levels_check.py Same translation refactor and minor function/label cleanup.
extensions/pyRevitTools.extension/checks/refplanes_check.py Same translation refactor and class metadata simplification.
extensions/pyRevitTools.extension/checks/schedules_not_on_sheet_check.py Same translation refactor and minor formatting cleanup.
extensions/pyRevitTools.extension/checks/worksets_content_check.py Same translation refactor; remove local path hack and metaclass usage.
extensions/pyRevitTools.extension/checks/walltypes_naming_convention_check.py Same translation refactor; remove local path hack and metaclass usage.
extensions/pyRevitTools.extension/checks/modelchecker_Warnings_check.py Same translation refactor; remove metaclass usage and minor formatting cleanup.
extensions/pyRevitTools.extension/checks/cad_audit_check.py Same translation refactor; replace translation calls with _t() and clean up formatting.
extensions/pyRevitTools.extension/checks/audit_all_check.py Same translation refactor; replace translation calls with _t() and remove metaclass usage.
extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.en_us.xaml Add new check-specific localization dictionary (English).
extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.de_de.xaml Add new check-specific localization dictionary (German).
extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.es_es.xaml Add new check-specific localization dictionary (Spanish).
extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.pt_br.xaml Add new check-specific localization dictionary (Brazilian Portuguese).
extensions/pyRevitTools.extension/checks/locale/Checks.ResourceDictionary.chinese_s.xaml Add new check-specific localization dictionary (Simplified Chinese).

Comment on lines +295 to +299
result = _read_key(path)
if result is not None:
return result
break # right locale file found but key missing — fall to English

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants